package com.zbkj.crmeb.task.plat.tiktok;

import com.alibaba.fastjson.JSON;
import com.doudian.open.api.brand_list.BrandListRequest;
import com.doudian.open.api.brand_list.BrandListResponse;
import com.doudian.open.api.brand_list.data.AuthBrandListItem;
import com.doudian.open.api.brand_list.data.BrandListData;
import com.doudian.open.api.brand_list.param.BrandListParam;
import com.doudian.open.api.shop_getShopCategory.ShopGetShopCategoryRequest;
import com.doudian.open.api.shop_getShopCategory.ShopGetShopCategoryResponse;
import com.doudian.open.api.shop_getShopCategory.data.DataItem;
import com.doudian.open.api.shop_getShopCategory.param.ShopGetShopCategoryParam;
import com.doudian.open.api.supplyChain_queryBrandList.SupplyChainQueryBrandListRequest;
import com.doudian.open.api.supplyChain_queryBrandList.SupplyChainQueryBrandListResponse;
import com.doudian.open.api.supplyChain_queryBrandList.data.BrandListItem;
import com.doudian.open.api.supplyChain_queryBrandList.param.SupplyChainQueryBrandListParam;
import com.doudian.open.api.warehouse_list.WarehouseListRequest;
import com.doudian.open.api.warehouse_list.WarehouseListResponse;
import com.doudian.open.api.warehouse_list.param.WarehouseListParam;
import com.doudian.open.core.AccessToken;
import com.doudian.open.core.AccessTokenBuilder;
import com.doudian.open.core.GlobalConfig;
import com.zbkj.crmeb.task.plat.poizon.enums.TikTokMerShopIdEnum;
import com.zbkj.crmeb.task.plat.tiktok.dto.TikTokGoodsCategoryDataItem;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zhangxuyang
 * @date 4/30/2024 2:31 下午
 */
@Component
@Slf4j
@Lazy
@Accessors(chain = true)
public class TikTokApi {

    public TikTokApi() {
        GlobalConfig.initAppKey("7361022555580548659");
        GlobalConfig.initAppSecret("ee0a6a8b-c6ec-48cd-902e-d383f4a20a50");
    }

    public List<com.doudian.open.api.brand_list.data.AuthBrandListItem> getAllBrand(Long categoryId, AccessToken accessToken) {
        BrandListRequest request = new BrandListRequest();
        BrandListParam param = request.getParam();
        param.setCategoryId(categoryId);
        BrandListResponse response = request.execute(accessToken);
        BrandListData data = response.getData();
        List<com.doudian.open.api.brand_list.data.AuthBrandListItem> brandList = data.getAuthBrandList();
        return brandList;
    }

    public List<TikTokGoodsCategoryDataItem> getAllCategory(Long id, AccessToken accessToken) {
        List<TikTokGoodsCategoryDataItem> TikTokGoodsCategoryDataItems = new ArrayList<>();
        ShopGetShopCategoryRequest request = new ShopGetShopCategoryRequest();
        ShopGetShopCategoryParam param = request.getParam();
        param.setCid(id);
        // 休眠一下 避免请求频繁 接口超时
        try {
            Thread.sleep((int) (Math.random() * 500) + 200);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        ShopGetShopCategoryResponse response = request.execute(accessToken);
        if (response.isSuccess()) {
            List<DataItem> data = response.getData();
            for (DataItem datum : data) {
                if (datum.getEnable()) {
                    TikTokGoodsCategoryDataItem TikTokGoodsCategoryDataItem = JSON.parseObject(JSON.toJSONString(datum), TikTokGoodsCategoryDataItem.class);
                    if (!datum.getIsLeaf()) {
                        TikTokGoodsCategoryDataItem.setChildren(getAllCategory(datum.getId(), accessToken));
                    }
                    TikTokGoodsCategoryDataItems.add(TikTokGoodsCategoryDataItem);
                }
            }
        }
        return TikTokGoodsCategoryDataItems;
    }

    public static Long getShopId(Integer merId) {
        return TikTokMerShopIdEnum.getShopIdByMerId(merId);
    }

    public static Integer getMerId() {
        return 0;
    }

    public static Integer getMerId_150() {
        return 150;
    }

    public static void main(String[] args) {
        TikTokApi tikTokApi = new TikTokApi();
        AccessToken accessToken = AccessTokenBuilder.build(getShopId(1));
        WarehouseListRequest request = new WarehouseListRequest();
        WarehouseListParam param = request.getParam();
        param.setPage(0L);
        param.setSize(10L);
        WarehouseListResponse response = request.execute(accessToken);
        System.out.println(JSON.toJSONString(response));
//        {
//            ShopGetShopCategoryRequest request = new ShopGetShopCategoryRequest();
//            ShopGetShopCategoryParam param = request.getParam();
//            param.setCid(20006L);
//            ShopGetShopCategoryResponse response = request.execute(accessToken);
//            System.out.println(response);
//        }
//        {
//            SupplyChainQueryCatePropListRequest request = new SupplyChainQueryCatePropListRequest();
//            SupplyChainQueryCatePropListParam param = request.getParam();
//            param.setCategoryId(20226L);
//            param.setBmpCode("xiaodian.crossborder");
//            SupplyChainQueryCatePropListResponse response = request.execute(accessToken);
//            String originResponse = response.getOriginResponse();
////            String originResponse = "{\"code\":10000,\"data\":{\"data\":{\"batch_prop_list\":[{\"prop_key\":{\"prop_code\":\"birth_date\",\"prop_key\":\"采集生产日期\",\"prop_key_id\":11},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":false,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Batch\"},\"prop_val\":[{\"prop_value\":\"是\",\"prop_value_id\":18},{\"prop_value\":\"否\",\"prop_value_id\":19}]},{\"prop_key\":{\"prop_code\":\"birth_batch\",\"prop_key\":\"生产批号是否采集\",\"prop_key_id\":21},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":false,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Batch\"},\"prop_val\":[{\"prop_value\":\"是\",\"prop_value_id\":20},{\"prop_value\":\"否\",\"prop_value_id\":21}]},{\"prop_key\":{\"prop_code\":\"expire_date\",\"prop_key\":\"采集失效日期\",\"prop_key_id\":22},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":false,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Batch\"},\"prop_val\":[{\"prop_value\":\"是\",\"prop_value_id\":22},{\"prop_value\":\"否\",\"prop_value_id\":23}]},{\"prop_key\":{\"prop_code\":\"scm_inbound_order_no\",\"prop_key\":\"入库业务单号\",\"prop_key_id\":47},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":false,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Batch\"},\"prop_val\":[{\"prop_value\":\"是\",\"prop_value_id\":49},{\"prop_value\":\"否\",\"prop_value_id\":50}]},{\"prop_key\":{\"prop_code\":\"scm_inbound_date_time\",\"prop_key\":\"入库日期\",\"prop_key_id\":90},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":false,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Batch\"},\"prop_val\":[{\"prop_value\":\"是\",\"prop_value_id\":88},{\"prop_value\":\"否\",\"prop_value_id\":89}]}],\"category_prop_list\":[{\"prop_key\":{\"prop_code\":\"brand\",\"prop_key\":\"品牌\",\"prop_key_id\":20},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"packaging_material\",\"prop_key\":\"包装材质\",\"prop_key_id\":27},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"公斤\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[{\"prop_value\":\"塑料\",\"prop_value_id\":90},{\"prop_value\":\"玻璃/陶瓷\",\"prop_value_id\":91},{\"prop_value\":\"塑料袋\",\"prop_value_id\":92},{\"prop_value\":\"铁盒\",\"prop_value_id\":93},{\"prop_value\":\"木盒\",\"prop_value_id\":94},{\"prop_value\":\"纸盒\",\"prop_value_id\":95},{\"prop_value\":\"套装\",\"prop_value_id\":96},{\"prop_value\":\"其他\",\"prop_value_id\":97}]},{\"prop_key\":{\"prop_code\":\"brand_location\",\"prop_key\":\"品牌所在地\",\"prop_key_id\":30},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"article_number\",\"prop_key\":\"货号\",\"prop_key_id\":34},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":false,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"fabric_material\",\"prop_key\":\"面料材质\",\"prop_key_id\":35},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":false,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"clothing_tech\",\"prop_key\":\"流行元素/工艺\",\"prop_key_id\":36},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":false,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"xianggui\",\"prop_key\":\"箱规\",\"prop_key_id\":49},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"is_expensive\",\"prop_key\":\"是否贵品\",\"prop_key_id\":50},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[{\"prop_value\":\"是\",\"prop_value_id\":53},{\"prop_value\":\"否\",\"prop_value_id\":54}]},{\"prop_key\":{\"prop_code\":\"is_breakable\",\"prop_key\":\"是否易碎品\",\"prop_key_id\":51},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[{\"prop_value\":\"是\",\"prop_value_id\":55},{\"prop_value\":\"否\",\"prop_value_id\":56}]},{\"prop_key\":{\"prop_code\":\"storage_type\",\"prop_key\":\"储存方式\",\"prop_key_id\":78},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[{\"prop_value\":\"常温\",\"prop_value_id\":73},{\"prop_value\":\"冷藏\",\"prop_value_id\":74},{\"prop_value\":\"冷冻\",\"prop_value_id\":75},{\"prop_value\":\"恒温\",\"prop_value_id\":87}]},{\"prop_key\":{\"prop_code\":\"is_special\",\"prop_key\":\"是否特殊品\",\"prop_key_id\":91},\"prop_meta\":{\"customize\":false,\"enumerable\":true,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[{\"prop_value\":\"是\",\"prop_value_id\":98},{\"prop_value\":\"否\",\"prop_value_id\":99}]}],\"period_prop_list\":[{\"prop_key\":{\"prop_code\":\"period_of_validity\",\"prop_key\":\"有效期(天)\",\"prop_key_id\":10},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":9999,\"measure_unit_name\":\"天\",\"min\":1,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"period_of_forbid_sale\",\"prop_key\":\"禁售期(天)\",\"prop_key_id\":19},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"天\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]}],\"sale_prop_list\":[{\"prop_key\":{\"prop_code\":\"0\",\"prop_key\":\"规格\",\"prop_key_id\":31},\"prop_meta\":{\"customize\":true,\"enumerable\":true,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":true,\"prop_type\":\"Sale\"},\"prop_val\":[]}],\"sku_category_prop_list\":[{\"prop_key\":{\"prop_code\":\"barcode\",\"prop_key\":\"条形码\",\"prop_key_id\":25},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":true,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"erp_sku_id\",\"prop_key\":\"外部货品编码\",\"prop_key_id\":29},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"通用\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"length\",\"prop_key\":\"长(mm)\",\"prop_key_id\":33},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"gross_weight\",\"prop_key\":\"毛重(g)\",\"prop_key_id\":72},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"wide\",\"prop_key\":\"宽(mm)\",\"prop_key_id\":74},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"high\",\"prop_key\":\"高(mm)\",\"prop_key_id\":75},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]},{\"prop_key\":{\"prop_code\":\"volume\",\"prop_key\":\"体积\",\"prop_key_id\":89},\"prop_meta\":{\"customize\":true,\"enumerable\":false,\"is_required\":true,\"max\":0,\"measure_unit_name\":\"厘米\",\"min\":0,\"multi_choice\":false,\"prop_type\":\"Common\"},\"prop_val\":[]}]}},\"log_id\":\"20240517095934AEF08F18BBFFB063D7F4\",\"msg\":\"success\",\"sub_code\":\"\",\"sub_msg\":\"\"}";
//
//            JSONObject jsonObject = JSON.parseObject(originResponse);
//            Gson r = new Gson();
//            SupplyChainQueryCatePropListResponse data = r.fromJson(originResponse, SupplyChainQueryCatePropListResponse.class);
////            System.out.println(response);
//        }
    }
    public List<TikTokGoodsCategoryDataItem> getList(List<TikTokGoodsCategoryDataItem> list,String prefixName) {
        List<TikTokGoodsCategoryDataItem> copyList = new ArrayList<>();
        if (StringUtils.isNotBlank(prefixName)) {
            prefixName = prefixName + "/";
        }
        for (TikTokGoodsCategoryDataItem categoryDataItem : list) {
            TikTokGoodsCategoryDataItem categoryDataItem1 = new TikTokGoodsCategoryDataItem();
            BeanUtils.copyProperties(categoryDataItem,categoryDataItem1);
            String finalPrefixName = prefixName;
            categoryDataItem1.setName(finalPrefixName + categoryDataItem.getName());
            if(!categoryDataItem.getIsLeaf()) {
                List<TikTokGoodsCategoryDataItem> list1 = getList(categoryDataItem.getChildren(),categoryDataItem.getName());
                list1.forEach(e -> e.setName(finalPrefixName + e.getName()));
                copyList.addAll(list1);
                categoryDataItem1.setChildren(null);
            }
            copyList.add(categoryDataItem1);
        }
        return copyList;
    }

    public List<DataItem> getTikTokGoodsCategoryList(Long cid, AccessToken accessToken) {
        ShopGetShopCategoryRequest request = new ShopGetShopCategoryRequest();
        ShopGetShopCategoryParam param = request.getParam();
        param.setCid(cid);
        ShopGetShopCategoryResponse response = request.execute(accessToken);
        if (response.isSuccess()) {
            return response.getData();
        }
        return null;
    }
}
